/************************************************************************************
 * @copyright Copyright 2024 Shanghai Fangling Software Co., Ltd. All Rights Reserved.
 * @file      AdjustSetArcPlus.qml
 * @version   V1.0
 * @brief     Adjust the settings for arc voltage 
 * @author    Zhijun Huo  ( zhijunhuo @flcnc.com)
 * @date      06/29/2024  (M/D/Y)
 * @details 
 * @par History
 * @others   get cur_set_arc | step | save | SetTable
 * Date  |  Author  |  Version  |  Description
 * ----  |  ------  |  -------  |  -----------
 * 06/29/2024  | Zhijun Huo  | V1.0 | Auto Arc THC Ui page
 *
 ***********************************************************************************/

import QtQuick 1.1
import com.flcnc.tube 1.0
import "../"

Rectangle {
  id: root_
  width: 250
  height: 100
  color: "gray"

  Item {

    Monitor {
      id: change_btn_name_
      timeout: 500
      repeat: false
      onUpdate: {
        rootOjbect.custom_name = qsTr("SetArc +")
      }
    }

    Monitor {

      running: rootOjbect.show
      onUpdate: {
        rootOjbect.show = false;
        operation_.getSetArcValue()

        operation_.adjustSetArcValue()
        operation_.save()
      }
    }
  }

  Item {
    id: operation_

    QtObject {
      id: internal_

      property string aUTOARCTHC_CUSTOM: qsTr("AutoArcTHC");
      // Define custom index
      property int arcTHC_custom_: -1;
      // step
      property int set_arc_step: 1
      property int fun_flag: 1
      property int set_arc: 0
      property int set_arc_index: -1
      property int aRC_TARGET_ADDR: 2680
    }

    Controller {
      id: control_
    }

    ConditionScript {
      id: condition_
    }

    Parameter {
      id: param_
    }

    XmlFunction {
      id: xml_
    }

    ZmcFunction {
      id: zmc_
    }

    Logger {
      id: logger_
    }
	
    function init(){
      operation_.getSetArcIndex();
      operation_.getSetArcValue();
    }

    /**
     * @brief 获取设置弧压index
    */
    function getSetArcIndex(){
      var custom_index = xml_.getCustomParamIndex(internal_.aUTOARCTHC_CUSTOM);
      internal_.arcTHC_custom_ = custom_index;
      if(custom_index < 0){
      	return -1;
      }
      internal_.set_arc_index = xml_.getParamIntIndex(custom_index, 1)
    }

    /**
     * @brief read setarc value
    */
    function getSetArcValue(){
      if (internal_.set_arc_index > 0) {
        internal_.set_arc = Number(param_.GetDirtyValue(internal_.set_arc_index));
      }
    }

    /**
     * @brief adjust setarc value
    */
    function adjustSetArcValue(){
      internal_.set_arc = internal_.fun_flag * internal_.set_arc_step + internal_.set_arc
    }

    /**
     * @brief save param，
    */
    function save() {
      param_.SetValue(internal_.set_arc_index,internal_.set_arc)
      param_.SaveParamToFile();
      //modify
      zmc_.setZmcTableValues(internal_.aRC_TARGET_ADDR, [internal_.set_arc]);
    }
  }
  Component.onCompleted: {
    operation_.init()
  }
}
